//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "uParmPrt.h"
//---------------------------------------------------------------------------
#define MODULE_TAG 1202
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
void __fastcall ExtractConstructeurPrinter( const AnsiString &strModele, AnsiString &strConstructeur, AnsiString &strPrinter)
{
  int     nPosSpc = strModele.Pos( " "); ASSERT( nPosSpc!=0);
  strConstructeur = strModele.SubString(         1, nPosSpc-1).Trim();
  strPrinter      = strModele.SubString( nPosSpc+1, strModele.Length()-nPosSpc).Trim();
}
//---------------------------------------------------------------------------
void __fastcall ParametrerImprimante( CBtrvFile *pBtrv, const AnsiString &strConstructeur, const AnsiString &strImprimante)
{
  TfrmParamPrinter *frmParamPrinter = NULL;
  try {
    frmParamPrinter = new TfrmParamPrinter( pBtrv, strConstructeur, strImprimante);
    frmParamPrinter->ShowModal();
  } catch ( Exception &e) {
    EICDisplayException( TAG( 5), e);
  }
  delete frmParamPrinter;
}
//---------------------------------------------------------------------------
__fastcall TfrmParamPrinter::TfrmParamPrinter( CBtrvFile *pBtrv,
                                               const AnsiString &strConstruc,
                                               const AnsiString &strImprim,
                                               TComponent* Owner /* = NULL */) : CForm(Owner)
{
  m_pBtrv                  = pBtrv;
  lblConstructeur->Caption = strConstruc;
  lblImprimante  ->Caption = strImprim;
}
//---------------------------------------------------------------------------
void __fastcall TfrmParamPrinter::DoDataExchange( CDataExchange* pdx)
{
  TDefPrinter printer, key;
  AnsiString  strConstructeur, strImprimante;
  AnsiString  strLignes, strColonnes;
  AnsiString  strCI[18];
  bool        bSkipPage;

  m_pBtrv->Open( TAG( 10), BTRV_RW, sizeof( TDefPrinter));
  m_pBtrv->GetFirst( TAG( 11), &printer, &key, 0);
  do {
    ExtractConstructeurPrinter( AnsiString( printer.szModele), strConstructeur, strImprimante);
    if (( lblConstructeur->Caption!=strConstructeur) || ( lblImprimante->Caption!=strImprimante)) continue;

    strLignes   = AnsiString( printer.sLignes  );
    strColonnes = AnsiString( printer.sColonnes);
    bSkipPage   =             printer.sSkipPage;
    for( int i=0; i<18; i++) strCI[i] = AnsiString(printer.szCI[i]).Trim();

    DDX_Text( pdx, edNbLines    , strLignes               );
    DDX_Text( pdx, edNbCols     , strColonnes             );
    DDX_Text( pdx, edInit       , strCI[         IDX_INIT]);
    DDX_Text( pdx, edReinit     , strCI[       IDX_REINIT]);
    DDX_Text( pdx, edInitCourier, strCI[ IDX_INIT_COURIER]);
    DDX_Text( pdx, edGrasDeb    , strCI[     IDX_GRAS_DEB]);
    DDX_Text( pdx, edGrasFin    , strCI[     IDX_GRAS_FIN]);
    DDX_Text( pdx, edItalikDeb  , strCI[   IDX_ITALIK_DEB]);
    DDX_Text( pdx, edItalikFin  , strCI[   IDX_ITALIK_FIN]);
    DDX_Text( pdx, edSoulDeb    , strCI[     IDX_SOUL_DEB]);
    DDX_Text( pdx, edSoulFin    , strCI[     IDX_SOUL_FIN]);
    DDX_Text( pdx, ed10CPI      , strCI[        IDX_10CPI]);
    DDX_Text( pdx, ed12CPI      , strCI[        IDX_12CPI]);
    DDX_Text( pdx, ed166CPI     , strCI[       IDX_166CPI]);
    DDX_Text( pdx, ed20CPI      , strCI[        IDX_20CPI]);
    DDX_Text( pdx, ed6LPI       , strCI[         IDX_6LPI]);
    DDX_Text( pdx, ed8LPI       , strCI[         IDX_8LPI]);
    DDX_Text( pdx, edPortrait   , strCI[     IDX_PORTRAIT]);
    DDX_Text( pdx, edPaysage    , strCI[      IDX_PAYSAGE]);
    DDX_IsChecked( pdx, cbPageSkip, bSkipPage);

    // Sur un UpdateData, on réécrit les nouvelles valeurs dans IMPRIMAN.DAT
    if ( !pdx->UpdateForm) {
      try { printer.sLignes   = (short)strLignes  .ToInt(); } catch( EConvertError &e) { pdx->Fail( "Saisie erronnée !\nVeuillez resaisir le nombre de lignes");   }
      try { printer.sColonnes = (short)strColonnes.ToInt(); } catch( EConvertError &e) { pdx->Fail( "Saisie erronnée !\nVeuillez resaisir le nombre de colonnes"); }
      printer.sSkipPage = (short)bSkipPage;
      for( int i=0; i<18; i++) strcpy( printer.szCI[i], strCI[i].Trim().c_str());
      m_pBtrv->UpdateCurrent( TAG( 14), (void *)&printer);
    }
    break;

  } while( m_pBtrv->GetNext( TAG( 12), &printer, &key, 0));
  m_pBtrv->Close( TAG( 13));
}
//---------------------------------------------------------------------------
void __fastcall TfrmParamPrinter::FormCloseQuery(TObject *Sender,
      bool &CanClose)
{
  if ( ModalResult==mrOk) CanClose = UpdateData();
}
//---------------------------------------------------------------------------

